package com.umeox.quranplayer.ui

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.umeox.moto.common.log.Logger
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.quranplayer.R
import com.umeox.quranplayer.bean.QuranChallenge
import com.umeox.watch.moto.dataservice.DataProvider
import java.util.*

class ChallengeTipActivity : ThemeAppCompatActivity() {

    private lateinit var mChallengeLevelDescTv: TextView
    private var mCurrentChallenge: QuranChallenge? = null
    private lateinit var mQuranNamesArr : Array<String>


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val bundle = intent.extras
        val _id = bundle?.getInt("id", 0) ?: 0
        Logger.e("_id=$_id")
        if (_id > 0) {
            mCurrentChallenge = getQuranChallengeById(_id)
        }
        if (mCurrentChallenge == null) {
            finish()
        }

        Logger.e("mCurrentChallenge=$mCurrentChallenge")

        setContentView(R.layout.activity_challenge_tip)
        mChallengeLevelDescTv = findViewById(R.id.tv_challenge_desc)
        mQuranNamesArr = resources.getStringArray(R.array.quran_name_arr)
        setChallengeDesc(mCurrentChallenge)
        findViewById<View>(R.id.iv_start).setOnClickListener {
            val intent = Intent(this, ChallengeActivity::class.java)
            intent.putExtra("challenge_data", mCurrentChallenge)
            startActivity(intent)
            finish()
        }
    }

    private fun getQuranChallengeById(_id: Int): QuranChallenge? {
        val values = DataProvider.getQuranLearnById(_id)
        if (values != null) {
            val quranChallenge = QuranChallenge()
            quranChallenge.id = values.getAsInteger(DataProvider.QuranLearnProvider.ID)
            quranChallenge.chapterIndex =
                values.getAsInteger(DataProvider.QuranLearnProvider.CHAPTER_INDEX)
            quranChallenge.duration = values.getAsInteger(DataProvider.QuranLearnProvider.DURATION)
            quranChallenge.deadline = values.getAsString(DataProvider.QuranLearnProvider.DEADLINE)
            quranChallenge.rewardValue = values.getAsInteger(DataProvider.QuranLearnProvider.REWARD_VALUE)
            return quranChallenge
        }
        return null
    }

    private fun setChallengeDesc(challengeBean: QuranChallenge?) {
        val challengeDescStr = getString(R.string.str_challenge_tip)
        mChallengeLevelDescTv.text =
            String.format(
                Locale.getDefault(),
                challengeDescStr,
                mQuranNamesArr[challengeBean?.chapterIndex!!],
                challengeBean.duration
            )
    }

}